package cn.jhc.crimeintent;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import cn.jhc.crimeintent.database.CrimeBaseHelper;
import cn.jhc.crimeintent.database.CrimeCursorWrapper;
import cn.jhc.crimeintent.database.CrimeDbSchema;
import cn.jhc.crimeintent.database.CrimeDbSchema.CrimeTable;
import cn.jhc.crimeintent.database.CrimeDbSchema.CrimeTable.Cols;

/**
 * Created by luyanfei on 16-10-20.
 */

public class CrimeLab {
    private static CrimeLab crimeLab;

    private SQLiteDatabase sqLiteDatabase;
    private final Context applicationContext;

    private CrimeLab(Context context) {
        applicationContext = context.getApplicationContext();
        sqLiteDatabase = new CrimeBaseHelper(applicationContext).getWritableDatabase();

    }

    public static CrimeLab get(Context context) {
        if(crimeLab == null) {
            crimeLab = new CrimeLab(context);
        }
        return crimeLab;
    }

    public void addCrime(Crime c) {
        ContentValues contentValues = getContentValues(c);
        sqLiteDatabase.insert(CrimeTable.NAME, null, contentValues);
    }

    public void updateCrime(Crime c) {
        String uuidString = c.getId().toString();
        ContentValues values = getContentValues(c);
        sqLiteDatabase.update(CrimeTable.NAME, values, Cols.UUID + "=?", new String[] {uuidString});
    }

    public List<Crime> getCrimes() {
        List<Crime> crimes = new ArrayList<>();
        CrimeCursorWrapper cursorWrapper = queryCrimes(null, null);
        cursorWrapper.moveToFirst();
        try {
            while (!cursorWrapper.isAfterLast()) {
                crimes.add(cursorWrapper.getCrime());
                cursorWrapper.moveToNext();
            }
        } finally {
            cursorWrapper.close();
        }
        return crimes;
    }

    public Crime getCrime(UUID uuid) {
        CrimeCursorWrapper cursorWrapper = queryCrimes(
                Cols.UUID + "=?",
                new String[] {uuid.toString()}
        );
        try {
            if(cursorWrapper.getCount() == 0) {
                return null;
            }
            cursorWrapper.moveToFirst();
            return cursorWrapper.getCrime();
        } finally {
            cursorWrapper.close();
        }

    }

    public File getPhotoFile(Crime crime) {
        File externalFileDir = applicationContext.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        if(externalFileDir == null)
            return null;
        return new File(externalFileDir, crime.getPhotoFilename());
    }

    private static ContentValues getContentValues(Crime crime) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(Cols.UUID, crime.getId().toString());
        contentValues.put(Cols.TITLE, crime.getTitle());
        contentValues.put(Cols.DATE, crime.getDate().getTime());
        contentValues.put(Cols.SOLVED, crime.isSolved() ? 1 : 0);
        contentValues.put(Cols.SUSPECT, crime.getSuspect());
        return contentValues;
    }

    private CrimeCursorWrapper queryCrimes(String whereClause, String[] whereArgs) {
        Cursor cursor = sqLiteDatabase.query(CrimeTable.NAME, null, whereClause, whereArgs, null, null, null);
        return new CrimeCursorWrapper(cursor);
    }
}
